package crowMessenger;

import java.util.Collection;
import java.util.Scanner;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;

public class CrowMain {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter Username: ");
		String username = scan.nextLine();
		System.out.print("Enter Password: ");
		String password = scan.nextLine();
		ConnectionConfiguration connectionConf = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
		XMPPConnection connection = new XMPPConnection(connectionConf);
		try {
			connection.connect();
		} catch (XMPPException e) {
			e.printStackTrace();
		}
		try {
			connection.login(username, password);
			System.out.println("Logged in as "+username);
			Presence presence = new Presence(Presence.Type.available);
			presence.setStatus("On with Crow-Testing...");
			connection.sendPacket(presence);
		} catch (XMPPException e) {
			e.printStackTrace();
		}
		Roster roster = connection.getRoster();
		Collection<RosterEntry> entries = roster.getEntries();
		for (RosterEntry entry : entries) {
			System.out.println(entry);
		}

		ChatManager chatmanager = connection.getChatManager();
		System.out.print("\nwhom do you like to chat with: ");
		String Dest = scan.nextLine();
		Chat newChat = chatmanager.createChat(Dest, new MessageListener() {
			public void processMessage(Chat chat, Message message) {
				System.out.println(message.getFrom()+" : "+message.getBody());
			}
		});
		connection.getChatManager().addChatListener(
				new ChatManagerListener() {
					public void chatCreated(Chat chat, boolean createdLocally)
					{
						if (!createdLocally)
							chat.addMessageListener(new MessageListener() {
								public void processMessage(Chat chat, Message message) {
									System.out.println(message.getFrom()+" : "+message.getBody());
								}
							});;
					}
				});
		while(true){
			String sendingMessage = scan.nextLine();
			if(sendingMessage!="end"){
				try {
					newChat.sendMessage(sendingMessage);
				} catch (XMPPException e) {
					e.printStackTrace();
				}
			}
			else if(sendingMessage == "end"){
				connection.disconnect();
			}
		}
	}
}
